﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LNFramework.Common.Extends;
using System.ServiceModel;
using Roy.IService;

namespace Roy.Site.Controllers
{
    public class HomeController : BaseController
    {
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 登录页
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            return View();
        }

        public JsonResult CheckLogin(string username, string password)
        {
            var flag = true;
            var msg = string.Empty;

            if (username.IsNullOrEmpty() || password.IsNullOrEmpty())
            {
                flag = false;
                msg = "用户名和密码不能為空!";
            }

            if (flag)
            {
                using (var factory = new ChannelFactory<IRoyUserService>("*"))
                {
                    try
                    {
                        var client = factory.CreateChannel();
                        var result = client.CheckLogin(username, password);
                        flag = result.Content;
                        msg = result.Message;
                    }
                    catch (Exception ex)
                    { 
                        //TODO:写错误日志

                        flag = false;
                        msg = "调用服务出错，请检查后重试!";
                    }
                }

            }

            return Json(new { Success = flag, Msg = msg}, JsonRequestBehavior.AllowGet);
        }

    }
}
